package pub.tbc.toolkit.web.config.rest.service.impl;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import pub.tbc.toolkit.exception.GlobalException;
import pub.tbc.toolkit.web.config.Config;
import pub.tbc.toolkit.web.config.PropertiesScan;
import pub.tbc.toolkit.web.config.rest.service.ConfigService;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author tbc tianbencai@e-eduspace.com
 * @version 1.0 {2016年5月18日 下午2:21:24}
 */
@Service("configService")
@SuppressWarnings("unused")
public class ConfigServiceImpl implements ConfigService {
    private Logger log = LoggerFactory.getLogger(ConfigServiceImpl.class);


    private String CLASS_PATH = getClass().getClassLoader().getResource("").getPath();

    @Override
    public Map<String, Set<String>> fileList() throws GlobalException {
        Set<String> set = Config.fileList();
        return Maps.newHashMap(ImmutableMap.of("configFiles", set));
    }

    @Override
    public Map<String, List<String>> propKeysByFile(String file) throws GlobalException {
        List<String> list = Config.propKeysByFile(file);
        return ImmutableMap.of("propList", list);
    }

    @Override
    public Map<String, Map<String, String>> propsByFile(String file) throws GlobalException {
        Map<String, String> map = Config.propsByFile(file);
        return ImmutableMap.of("props", map);
    }

    @Override
    public Map<String, String> refresh() throws GlobalException {
        PropertiesScan.scan();
        return ImmutableMap.of("code", "200");
    }

    @Override
    public Map<String, String> update(String configFile, String propName, String value) throws GlobalException {
        Config.setProper(configFile, propName, value);
        return ImmutableMap.of("code", "200");
    }

}
